package com.binsoft.ndtta;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class TeamViewActivity extends Activity 

{
	private static final String TAG = "TeamViewActivity";
	private TextView mLandLineField = null;
	private TextView mMobileField = null;
	private TextView mWorkField = null;
	private TextView mEmailField = null;
	private Team mTeam = null;
	private int mBundleTeam = 0;
	private int mBundleDivision = 0;
	
	private Map< View, View > mLayoutPairs =  new HashMap<View, View>();
	
	private OnClickListener mClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) 
		{
			String numberString = "";
			String emailAddr = "";
			
			boolean mailer = false;
			boolean dialer = false;
			
			if ( v.getId() == R.id.teamViewLandLine )
			{	
				numberString = mLandLineField.getText().toString();
				dialer = true;
			}
			else if ( v.getId() == R.id.teamViewMobile )
			{
				numberString = mMobileField.getText().toString();
				dialer = true;
			}
			else if ( v.getId() == R.id.teamViewWork )
			{
				numberString = mWorkField.getText().toString();
				dialer = true;
			}
			else if ( v.getId() == R.id.teamViewEmail )
			{
				emailAddr = mEmailField.getText().toString();
				mailer = true;
			}

			Intent intent = null;
			
			if ( dialer )
			{
				intent = new Intent( Intent.ACTION_DIAL, Uri.parse("tel:" + numberString) );
			}
			else if ( mailer )
			{
				// Setup a pending event for the email button
				final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
				emailIntent.setType("plain/text");
				emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailAddr});
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "NDTTA");
				intent = Intent.createChooser(emailIntent, "Email:");
			}
			
			if ( intent  != null )
			{
				startActivity( intent );
			}
		}
	};
	
	
	private void setTextViewText( TextView view, byte[] text, byte[] key )
	{
		byte[] decrypted = null;
		
		if ( null != text )
		{
			Log.i(TAG, "Key:" + key );

			decrypted = Encryptor.decrypt(text, key );
		}

		if ( null != decrypted )
		{
			view.setText( new String( decrypted ) );
		}
		else
		{
			view.setVisibility( View.GONE );
			
			// Find the corresponding label
			View labelView = mLayoutPairs.get( view );
			labelView.setVisibility( View.GONE );
		}
	
		view.setOnClickListener( mClickListener );

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.team_view );

		// Get the values from the intent
		Bundle extras = getIntent().getExtras();
		
		// Extract the team
		mBundleTeam = extras.getInt(Constants.TEAM_INTENT_EXTRA);
		mBundleDivision = extras.getInt(Constants.DIVISION_INTENT_EXTRA);
		byte [] decryptionKey = extras.getByteArray( Constants.ENCRYPTION_KEY_EXTRA );
		
		mTeam = Matches.getTeam(mBundleDivision, mBundleTeam );
		
		((TextView)findViewById(R.id.teamViewTeam)).setText( mTeam.name() );
		
		TeamContact contact = mTeam.contact();
		
		setTextViewText(((TextView)findViewById(R.id.teamViewContact)), contact.encName(), decryptionKey);
		((TextView)findViewById(R.id.teamViewHomeNight)).setText( convertDayofWeek( mTeam.homeNight() ) );

		// Store the fields for later.
		mLandLineField = (TextView)findViewById(R.id.teamViewLandLine);
		mMobileField = ((TextView)findViewById(R.id.teamViewMobile));
		mWorkField = ((TextView)findViewById(R.id.teamViewWork));
		mEmailField = ((TextView)findViewById(R.id.teamViewEmail));

		// Setup the pairs
		mLayoutPairs.put( mLandLineField, (TextView)findViewById(R.id.telephoneLabel));
		mLayoutPairs.put( mMobileField, (TextView)findViewById(R.id.mobileLabel));
		mLayoutPairs.put( mWorkField, (TextView)findViewById(R.id.workLabel));
		mLayoutPairs.put( mEmailField, (TextView)findViewById(R.id.emailLabel));
		
		// Set the text
		setTextViewText( mLandLineField, contact.encLandline(), decryptionKey );
		setTextViewText( mMobileField, contact.encMobile(), decryptionKey );
		setTextViewText( mWorkField, contact.encWork(), decryptionKey );
		setTextViewText( mEmailField, contact.encEmail(), decryptionKey );
	}
	
	private String convertDayofWeek( int day )
	{
		switch ( day )
		{
		case Calendar.MONDAY: return "Monday";
		case Calendar.TUESDAY: return "Tuesday";
		case Calendar.WEDNESDAY: return "Wednesday";
		case Calendar.THURSDAY: return "Thursday";
		case Calendar.FRIDAY: return "Friday";
		default: return "Unknown";
		}
	}

}
